import { get, fnChoose, lsGetStr, fnUpload, post } from '../../utils/request.js';
import _ from "../../utils/underscore.js";
var util = require("../../utils/util.js");
var d = require("../../pages/wxParse/wxParse.js");
import { getType } from "../../utils/data_type.js";
var { getLoginedUser, setTitle, fnLoading, fnHideLoading, fnSuccess, fnFail } = require('../../utils/index');
import { checkLoginAsyc } from '../../utils/util.js';

Component({
  properties: {
    typeId:{
      type:String,
      value:'',
    },
    options:{
      type:String,
      value:''
    },
    butLabel:{
      type:String,
      value:''
    }
  },
  data: {
    typeId:'',
    options:'',
    hidden:true,
    hasUserInfo: false,
    cart: false,
    open: "",
    butLabel:'',
    
  },
  lifetimes: {
    attached: function () {
    },
    moved: function () {
    },
    detached: function () {
    },
  },
  attached() {
  }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function () { 
    let that = this;
    that.setData({
      typeId: that.properties.typeId||'',
      options: that.properties.options || '',
      butLabel: that.properties.butLabel || ''
    })
  },
  methods: {
    // 申请按钮
    navTo() {
      let that = this;
      get({
        path: "user/getInfo"
      }).then(function (res) {
        // console.log(res)
        if (!res.user.phone) {
          that.setData({
            verifyPhone: true,
          })
        } else {
          wx.navigateTo({
            url: '/pages/certificate/index?type=14184',
          })
        }
      })
    },
    // 提交按钮
    button: function () {
      let that = this;
      let other = _.filter(this.data.other, function (item) {
        return !!item.selected;
      });
      if (other.length < this.data.other.length) {
        return fnFail('请完成问卷后提交～');
      }
      
      if (that.data.open == 14184) {
        post({
          path: "/order/apply",
          data: {
            skey: lsGetStr('skey'),
            type: 0,
            shopIds: _.pluck(this.data.other, 'id').join(','),
            numbers: _.pluck(this.data.other, 'selected').join(',')
          }
        }).then(function (res) {
          fnSuccess("提交成功.....");
          setTimeout(function () {
            // 软著时跳转至一个新页面
            wx.redirectTo({
              url: "../certificate-share/index?type1=11"
            })
          }, 1000)
        }).catch(function (error) {
          fnFail('请勾选～')
        });
      } else if (that.data.open == 87) {
        post({
          path: "/order/apply",
          data: {
            skey: lsGetStr('skey'),
            type: 1,
            shopIds: _.pluck(this.data.other, 'id').join(','),
            numbers: _.pluck(this.data.other, 'selected').join(',')
          }
        }).then(function (res) {
          fnSuccess("提交成功.....");
          wx.redirectTo({
            url: "../certificate-share/index?type1=12"
          })

        })
      } else {
        post({
          path: "/order/apply",
          data: {
            skey: lsGetStr('skey'),
            type: 2,
            shopIds: _.pluck(this.data.other, 'id').join(','),
            numbers: _.pluck(this.data.other, 'selected').join(',')
          }
        }).then(function (res) {
          fnSuccess("提交成功.....");
          setTimeout(function () {
            wx.redirectTo({
              // url: "../my-service/index"
              url: "../certificate-share/index?type1=13"
            })
          }, 1000)

        })
      }
    },
    // 加入购物车按钮
    but: function (e) {
      debugger
      let that = this;
      if (this.data.butLabel == '加入购物车') {
        return checkLoginAsyc()
          .then(function () {
            post({
              path: "/order/addShoppingcart",
              data: {
                offerId: that.data.id,
                number: 1,
                skey: lsGetStr('skey')
              }
            })
          })
          .then(function (res) {
            fnSuccess("加入成功....")
          })
          .catch(function () {
            fnFail('加入购物车失败～')
          })
      }
      if (this.data.id == 84) {
        wx.navigateTo({
          url: '../certificate/index?type=' + this.data.id,
        });
      } else {
        wx.navigateTo({
          url: '../certificate/index?type=' + this.data.id,
        });
      }
    },
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      let that = this
      console.log(11, that.data.butLabel)
      if (that.data.typeId == '14225') {
        that.setData({
          cart:true,
          hidden: false,
          hasUserInfo: false
        })
      } else if(that.data.typeId == '979'){
        that.setData({
          cart: false,
          hidden: true,
          hasUserInfo: false
        })
      } else if (that.data.options == '14184'){
        that.setData({
          cart: false,
          hidden: false,
          hasUserInfo:true,
          open:that.data.options
        })
      } else if (that.data.butLabel == "申请"){
        that.setData({
          cart: false,
          hidden: false,
          hasUserInfo: false,
        })
          
      }
     
    },
  },
})